$(document).ready(function(){
//var info = [
//		[
//			{"id":"第1位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第2位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第3位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第4位","name":"sss","url":"images/seated_bg.png"},
//			{"id":"第5位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第6位","name":"sss","url":"/Public/game/images/0.jpg"}
//		],
//		[
//			{"id":"第1位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第2位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第3位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第4位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第5位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第6位","name":"sss","url":"/Public/game/images/0.jpg"},
//		],
//		[
//			{"id":"第1位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第2位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第3位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第4位","name":"sss","url":"/Public/game/images/0.jpg"},
//			{"id":"第5位","name":"sss","url":"images/seated_bg.png"},
//			{"id":"第6位","name":"sss","url":"/Public/game/images/0.jpg"},
//		]
//	];

	var centerPos = {"x":document.body.clientWidth*.5,"y":document.body.clientHeight*.5}
	

	var cur = userUrl;
	var canclick = true;
	var aniTime = 300;
	$(".content").on("touchstart",".desk:not(:last)",function(e){
	  if(canclick){
		canclick = false;
		var _s = $(this);
	  	//显示遮罩
		_s.after('<div class="desk_mask"></div>');
		
		
		//移除滚动
		$(".content").on("touchmove",function(e){
			e.preventDefault();
			e.stopPropagation();
		});
		
		//把其它桌子隐藏
		_s.siblings().filter(':not(".desk_mask")').transition({"opacity":0,},aniTime)
		//把头和尾内容隐藏
		_s.parent().siblings().filter(':not(".mask")').css("opacity","0");
		
		var offsetXY = _s.offset();
		//计算原始缩放
		if(_s.data("index")==0){
			zoom = 1.3;
		}else{
			zoom = 1;
		}
		//计算最终偏移值
		var offsetY =centerPos.y- parseFloat(offsetXY.top)-_s.height()*zoom*.5-100,
			offsetX =centerPos.x- parseFloat(offsetXY.left)-_s.width()*zoom*.5;
		$(".mask").show();
		$(this).transition({"x":offsetX,"y":offsetY,"scale":2.2},aniTime,function(){
			_s.css("z-index",2);
			//!!!!!!!!!!!!!!!!!给未坐下的位置添加动画
			$(this).find(".seat").has("img[src='/Public/game/images/seated_bg.png']").addClass("animated tada infinite");
//			_s.children(".cencir").removeClass("animated");
			
			//添加椅子事件
			_s.on("touchstart", ".seat",seatClickFun);
			//点击空白恢复原来大小
			$(".desk_mask").one("touchstart",function(){
				$(".desk").off("touchstart", ".seat");
				//!!!!!!!!!!!移除空位动画
				_s.find(".seat").removeClass("animated tada infinite");
//				_s.children(".cencir").addClass("animated");
				
				
				_s.transition({"x":0,"y":0,"scale":zoom},aniTime,function(){
					
					//恢复滚动
					$(".content").off("touchmove");
					canclick = true;
					_s.css("z-index",0);
					$(".desk_mask").remove();
				}
				).siblings().transition({"opacity":1,},aniTime)
				
				//回复点击前的style
				$(".desk").parent().siblings().css("opacity","1");
				$(".mask").hide();
				
			})
			
		});
		
	  }
	})
	
	
	//实现点击最后一个增加一张桌子
	$(" .desk:last").on("touchstart",function(e){
//		alert(11)
		e.stopPropagation();
		//判断是否除主宾坐外还有没有空位
		var canseated = true;
		$(".desk img").each(function(k,v){
				if($(v).attr("src")== "/Public/game/images/seated_bg.png"){
					canseated = false;
					return false;
				}else{canseated = true};
		})
		if(canseated){
//			alert(canseated);
			var j = $(" .desk").length-1;
			var clone = $(" .desk").eq(1).clone();
			$(this).before(clone);
			//设置新增加桌子的index为当前长度-2
			clone.attr("data-index",$(" .desk").length-2);
			//清空座位图片
			clone.find("img").attr("src","/Public/game/images/seated_bg.png");
			//改变圆桌内容
			clone.find(".cencir p").text("第"+j+"桌");
			//添加一个桌子的json数据
			var addjson = [{
					"id": "第1位",
					"name": "sss",
					"url": "/Public/game/images/0.jpg"
				}, {
					"id": "第2位",
					"name": "sss",
					"url": "/Public/game/images/0.jpg"
				}, {
					"id": "第3位",
					"name": "sss",
					"url": "/Public/game/images/0.jpg"
				}, {
					"id": "第4位",
					"name": "sss",
					"url": "/Public/game/images/0.jpg"
				}, {
					"id": "第5位",
					"name": "sss",
					"url": "/Public/game/images/0.jpg"
				}, {
					"id": "第6位",
					"name": "sss",
					"url": "images/seated_bg.png"
				}, ];
				jsonDataObj.push(addjson);
				
				
		}else{
			showwel(5,"还有空座位哦!");
		};
	})
	
	
	//判断info信息里面桌子个数,桌子自动匹配
	var info_length =jsonDataObj.length;
	while(info_length >	$(".desk").length-1){
		var j = $(".desk").length-1;
		var clone = $(".desk").eq(1).clone();
		$(".desk:last").before(clone);
		//设置新增加桌子的index为当前长度-2
		clone.attr("data-index",$(".desk").length-2);
		//清空座位图片
		clone.find("img").attr("src","/Public/game/images/seated_bg.png");
		//改变圆桌内容
		clone.find(".cencir p").text("第"+j+"席");
	};
		$(".seat img").each(function(){
			var sel_index = $(this).parent().data("index");
			var fa_index = $(this).parents(".desk").data("index");
//			console.log(sel_index,fa_index);
			if(fa_index<=info_length-1){
				$(this).attr("src",jsonDataObj[fa_index][sel_index].url);
			};
		})
		
		//动态聊天
		function wel(){
			var ran = $(".seat img:not([src='/Public/game/images/seated_bg.png'])").length;
			setInterval(function(){
				var arr=["...-_-","你也来!","你好!","快进来!"];
				var suiji1 = parseInt(Math.random()*arr.length);
				
				
				var div = "<div class='tem' style = ' text-align:center;position:absolute;top:-25px;right:-54px;color:#e7c598;background:url(/Public/game/images/QiPao.png) left top/100% 100%;font-size:24px; width:90px; height:50px;line-height:35px'>"+arr[suiji1]+"</div>";
				var suiji = parseInt(Math.random()*ran);
				if(suiji==0){
					return;
				}
				$(".seat img").not("img[src='/Public/game/images/seated_bg.png']").parent().eq(suiji).append(div);
				$(".tem").transition({"scale":1},3000,function(){
					$(this).remove();
				});
			},3000)
		}
		
		wel();
		
		
	
//点击移除进入时的遮罩
$(".remove").on("touchstart",function(){
	$(".welcome").css("display","none");
});


//封装函数用于触发关闭遮罩

//关闭欢迎弹出
function close(){
	$(".welcome").css("display","none");
};
//close();
//

//当页面不是第一个首页及不是生成宴席页面时,要隐藏头部title和底部按钮
	function hidefooterbtn(){
		$(".footer .btn").css("display","none");
		$(".footer .btns_f").css("display","block");
	}

	//显示已经坐下的人数
	var onseatnum =$(".seated").has("img[src='/Public/game/images/seated_bg.png']").length;
	$(".footer .btns_f input:first").val(onseatnum+1);
	$(".footer .btns_f input:last").val(onseatnum);
	
	$(".desk_mask").on("touchstart",function(e){
		e.preventDefault();
		e.stopPropagation();
	})
});
